﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace AiFrame.Interface.WinUI.Windows.Controls
{
    public partial class GradientPanel : System.Windows.Forms.Panel
    {
        private static Color _startColor;
        private static Color _endColor;
        
        public GradientPanel()
        {
            InitializeComponent();
            GradientActive = true;
            PaintGradient();
        }

        public static void InitializeColors(Color startColor, Color endColor)
        {
            _startColor = startColor;
            _endColor = endColor;
        }

        public Color GradientStartColor
        {
            get
            {
                return _startColor;
            }
            set
            {
                _startColor = value;
                PaintGradient();
            }
        }

        public Color GradientEndColor
        {
            get
            {
                return _endColor;
            }
            set
            {
                _endColor = value;
                PaintGradient();
            }
        }

        public bool GradientActive { get; set; }

        private void PaintGradient()
        {
            if (GradientActive)
            {
                System.Drawing.Drawing2D.LinearGradientBrush gradientBrush;
                gradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), _startColor,
                                                        _endColor);

                Bitmap bmp = new Bitmap(this.Width, this.Height);
                Graphics g = Graphics.FromImage(bmp);
                g.FillRectangle(gradientBrush, new Rectangle(0, 0, this.Width, this.Height));
                this.BackgroundImage = bmp;
                this.BackgroundImageLayout = ImageLayout.Stretch;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}